using AutoMapper;
using Crossborder.Core.Cto;
using Crossborder.Core.Dto;
using Crossborder.Domian.Orders;
using Crossborder.Domian.Products;
using Crossborder.Domian.Users;
using Newtonsoft.Json;
using StackExchange.Redis;

namespace Crossborder.Service.Base
{
    public class CrossBorderProfile : Profile
    {
        public CrossBorderProfile()
        {
            #region User 

            CreateMap<UserDto, User>().ReverseMap();
            CreateMap<UserInputDto, User>();
            CreateMap<UserUpdateDto, User>();
            CreateMap<LoginInputDto, User>();

            #endregion

            #region Product

            CreateMap<CategoryDto, Categorys>().ReverseMap();
            CreateMap<CategoryInputDto, Categorys>();
            CreateMap<ProductDto, ProductInfos>().ReverseMap();
            CreateMap<ProductInputDto, ProductInfos>();
            CreateMap<ProductClassifyDto, ProductClassify>().ReverseMap();
            CreateMap<ProductClassifyInputDto, ProductClassify>();

            #endregion

            #region ShoppingCart

            CreateMap<ProductDto, ShoppingCartCto>()
            .ForMember(cto => cto.Photos, m => m.MapFrom(dto => JsonConvert.SerializeObject(dto.Photos)))
            .ForMember(cto => cto.Classify, m => m.MapFrom(dto => JsonConvert.SerializeObject(dto.Classify)));

            CreateMap<ShoppingCartCto, ProductDto>()
            .ForMember(dto => dto.Photos, m => m.MapFrom(cto => JsonConvert.DeserializeObject(cto.Photos)))
            .ForMember(dto => dto.Classify, m => m.MapFrom(cto => JsonConvert.DeserializeObject(cto.Classify)));

            CreateMap<ShoppingCartCto, ShoppingCartDto>()
            .ForMember(dto => dto.Photos, m => m.MapFrom(cto => JsonConvert.DeserializeObject(cto.Photos)))
            .ForMember(dto => dto.Classify, m => m.MapFrom(cto => JsonConvert.DeserializeObject(cto.Classify)));

            CreateMap<ShoppingCartInputDto, ShoppingCartCto>()
            .ForMember(cto => cto.Photos, m => m.MapFrom(dto => JsonConvert.SerializeObject(dto.Photos)))
            .ForMember(cto => cto.Classify, m => m.MapFrom(dto => JsonConvert.SerializeObject(dto.Classify)));


            #endregion

            #region Order
            CreateMap<OrderMaster, OrderMasterDto>().ReverseMap();
            CreateMap<OrderMasterInputDto, OrderMaster>();
            CreateMap<ShoppingCartDto, OrderDetail>()
            .ForMember(o => o.ClassifyName, m => m.MapFrom(dto => dto.Classify.ClassifyName))
            .ForMember(o => o.ProductImageUrl, m => m.MapFrom(dto => dto.Photos.ImageUrl))
            .ForMember(o => o.Price, m => m.MapFrom(dto => dto.Classify.Price));
            #endregion
        }
    }
}